import sys                        
import pygame                     
from pygame.locals import * #constants from pygame.locals - like OPENGL, K_1, etc...
import pygame.font as font
from OpenGL.GL import *  # glSomething
from OpenGL.GLU import *
from random import randint
import random
import numpy as np
import math
import objloader
import geometry

sqr = lambda x: x*x

if True: #fullscreen
	fs = FULLSCREEN
	RESOLUTION = (1280,800)
else:
	RESOLUTION = (800,600)
	fs = 0

pygame.init()  # kickstart pygame
pygame.display.set_caption("Hello 3D!")
screen= pygame.display.set_mode(RESOLUTION,OPENGL|DOUBLEBUF|fs)

glPointSize(3)
glEnable(GL_LIGHTING)
glLightfv(GL_LIGHT0, GL_POSITION,  (-40, 200, 100, 0.0))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
glEnable(GL_LIGHT0)
glEnable(GL_DEPTH_TEST)

class palette:
	colors = [(0,1,1),(1,0,1),(1,1,0)]

polygonmode = 0
def changemode():
	global polygonmode
	if polygonmode ==0:
		glPolygonMode(GL_FRONT,GL_LINE)
		glPolygonMode(GL_BACK,GL_LINE)
		polygonmode = 1
	elif polygonmode == 1:
		glPolygonMode(GL_FRONT,GL_FILL)
		glPolygonMode(GL_BACK,GL_FILL)
		polygonmode = 2
	elif polygonmode == 2:
		glPolygonMode(GL_FRONT,GL_POINT)
		glPolygonMode(GL_BACK,GL_POINT)
		polygonmode = 0

def reSetProjection():
	glMatrixMode(GL_PROJECTION)             # Select GL_PROJECTION matrix
	glLoadIdentity()                        # Reset GL_PROJECTION matrix
	gluPerspective(45,RESOLUTION[0]/RESOLUTION[1],2,100)

reSetProjection()
glClearColor(0.0,0.0,0.0,0.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glEnable(GL_COLOR_MATERIAL)


cowlist = objloader.OBJ("models/cow2.obj").gl_list

def cube(x,y,z,sc = 0.5):
	glPushMatrix()
	glTranslatef(x,y,z)
	glScalef(sc,sc,sc)
	glCallList(cowlist)
	glPopMatrix()

SX = 10
SY = 10
field = np.zeros((SX,SY),dtype=int)
field[SX/4,SY/5] = 1

from camera import Camera
camera = Camera(5,2,5,0)
camera.pitch(-1)

def evalfile(fn):
	return eval(open(fn,"r").read())

points = evalfile("models/tetrahedtron.txt")

def draw ():
	glClear(GL_COLOR_BUFFER_BIT)
	camera.lookat()
	glDisable(GL_LIGHTING)
	glBegin(GL_LINES)
	for i in xrange(-10,11):
		if i % 10 == 0:
			glColor(0.0,1.0,0.0)
		else: 
			glColor(0.0,0.3,0.0)
		glVertex(i,0,-20)
		glVertex(i,0,20)

		glVertex(-20,0,i)
		glVertex(20,0,i)
	glEnd()
	glEnable(GL_LIGHTING)
	glColor(1,1,1)
	cube(0,0,0,1)
	glClear(GL_DEPTH_BUFFER_BIT)

	glColor(1,0,0)
	glBegin(GL_POINTS)
	for i in points:
		glVertex(i[0],i[1],i[2])
	glEnd()

	glFlush()                                # Flush everything to screen ASAP
	pygame.display.flip()                    # Flip the double-buffer

pygame.key.set_repeat(300,50)
pygame.mouse.set_visible(False)

pygame.event.set_allowed(None)
pygame.event.set_allowed([QUIT,MOUSEMOTION,MOUSEBUTTONDOWN])


def processinput():
	event=pygame.event.poll()

	if event.type is QUIT:
		sys.exit(0)
	key = pygame.key.get_pressed()
	if key[K_ESCAPE]: sys.exit(0)
	if key[K_w] : camera.forward(0.5)
	if key[K_s] : camera.forward(-0.5)
	if key[K_LEFT]: camera.steer(-0.05)
	if key[K_RIGHT]: camera.steer(0.05)
	if key[K_r] : camera.lift(0.2)
	if key[K_f] : camera.lift(-0.2)
	if key[K_e] : camera.point_at(0,0,0)
	if key[K_m]: changemode()

	if event.type is MOUSEMOTION:
		x,y = pygame.mouse.get_rel()
		camera.steer(x/300.0)
		camera.pitch(-y/300.0)
while 1:
	processinput()
	draw()
